package karileet.meeting.profile.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import karileet.meeting.profile.MeetingProfile;
import karileet.meeting.profile.ProfileConf;
import karileet.meeting.profile.model.Weekdays;

import java.util.Calendar;

public abstract class AbstractScheduleService extends Service {
    protected ProfileConf config;
    protected MeetingProfile mp;

    @Override
    public void onCreate() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.config = new ProfileConf(getApplicationContext());
        this.mp = new MeetingProfile(getApplicationContext());
        super.onStartCommand(intent, flags, startId);
        if (intent != null) {
            initiate();
        } else {
            Log.d(ProfileConf.APP, "Skiped start command, since intent was null");
        }
        return START_STICKY;
    }

    /**
     * Initiate profile automatic start or stop.
     */
    abstract public void initiate();

    protected boolean isDayEnabled() {
        Log.d(ProfileConf.APP, "Checking if day is enabled");
        Calendar calendar = Calendar.getInstance();
        /*
         * DOW        POSITION
         * 1 - Sunday    6
         * 2 - Monday    0
         * 3 - Tuesday   1
         * 4 - Wednesday 2
         * 5 - Thursday  3
         * 6 - Friday    4
         * 7 - Saturday  5
          */

        // I am converting Calendar DOW to my DOW ordering by subtracting 2. If number is negative, then the DOW is
        // sunday and it's value is 6
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 2;
        if (dayOfWeek < 0) {
            dayOfWeek = 6;
        }

        Weekdays weekdays = config.getWeekdays();
        Log.d(ProfileConf.APP, "Today is the " + dayOfWeek + "th day of the week");
        return weekdays.isDayEnabled(dayOfWeek);
    }

}